#include <iostream>
#include <algorithm>
#include <cmath>
using namespace std;

struct Point{
	int x;
	int y;
};
struct Rule1
{
	bool operator()(const int &x,const int &y)
	const{
		if(x%10!=y%10)
			return (x%10)<(y%10);
		return x>y;
	}
};
struct Rule2
{
	bool operator()(const Point &m,const Point &n)
	const{
		int a=(m.x)*(m.x)+(m.y)*(m.y);
		int b=(n.x)*(n.x)+(n.y)*(n.y);
		if(a==b)
		{
			if(m.x==n.x)
				return m.y<n.y;
			return m.x<n.x;
		}
		return a<b;
	}
};
int main()
{
	int a[8] = {6,5,55,23,3,9,87,10 };
	sort(a,a+8,Rule1());
	for(int i = 0;i < 8; ++i)
		cout << a[i] << "," ; 
	cout << endl;
	Point ps[8] = {{1,0},{0,1},{0,-1},{-1,0},{1,-1},{1,1},{2,0},{-2,0} } ;
	sort(ps,ps+8,Rule2());
	for(int i = 0;i < 8; ++i)
		cout << "(" << ps[i].x << "," << ps[i].y << ")"; 
	return 0;
}
